
var NewsModel = require('../Dao/newsdao');
var JsonModel = require('../config/jsonModel');
var CheckLogin = require('./checkLogin').checkAdminLogin;

module.exports = function(app) {



  // ----------------- 添加一条新闻
  app.post('/addNews', CheckLogin, function(req, res, next) {
    console.log('12312312');
    var model = new JsonModel.JsonModel();

    var title = req.body.title;
    var content = req.body.content;
    var type = Number(req.body.type) || -1;

    model.status = 0;

    try {
      if (!(title.length >= 1 && title.length < 100)) {
        throw new Error('文章标题在1-100字!');
      }
      if (!(content.length >= 1 && content.length < 10000000)) {
        throw new Error('文章内容不能为空!');
      }

      if (type === -1) {
        throw new Error('选择用户权限等级!');
      }
    } catch (e) {
      model.content = e.message;
      model.msg = e.message;
      return res.send(model);
    }


    var news = {
      author: 'admin',
      title: title,
      content: content,
      type: type,
      pv: 0
    };
    NewsModel.create(news)
        .then(function (result) {
          console.log(result.ops[0]);
          console.log('插入成功!');

          var model = new JsonModel.JsonModel();
          model.msg = '成功';
          model.content = '插入成功!';
          res.send(model);

    }).catch(function(err){

      model.msg = JSON.stringify(err);
      model.content = "操作失败";
      res.send(model);
    });
  });

  // ----------------- 获取所有新闻
  app.get('/getAllNews', function (req, res) {
      NewsModel.getAllNews().then(function (news) {
        var model = new JsonModel.JsonModel();
        model.msg = '成功';
        model.content = {newsList: news};
        return res.send(model);
      });
  });

  // ----------------- 根据用户名获取新闻
  app.get('/getAllNewsByAuthor', function (req, res) {
    NewsModel.getNewsByAuthor('afer').then(function (news) {
      console.log(news.count);
      var model = new JsonModel.JsonModel();
      model.msg = '成功';
      model.content = news;
      res.send(model);
    })
  });

  app.get('/getNewsByType', function (req, res) {
    var model = new JsonModel.JsonModel();
    model.status = 0;
    var type = req.query.type || -1;
    console.log(req.query.type);
    try {
      if (type === -1) {
        throw new Error('类别不能为空!');
      }
    } catch (e) {
      model.content = e.message;
      model.msg = e.message;
      return res.send(model);
    }

    NewsModel.getNewsByType(type).then(function (news) {
      var model = new JsonModel.JsonModel();
      model.msg = '成功';
      model.content = news;
      res.send(model);
    })
  })

  // ----------------- 根据删除某用户的所有新闻
  app.get('/delAllByAuthor', function (req, res) {
    var model = new JsonModel.JsonModel();
    NewsModel.delNewsByAuthor('12312').then(function () {
      model.msg = '成功';
      model.content = '删除成功';
      res.send(model);
    }).catch(function (error) {
      model.msg = JSON.stringify(err);
      model.content = "操作失败";
      res.send(model);
    });
  });

    app.get('/getNewsByID', function (req, res) {
        var model = new JsonModel.JsonModel();
        model.status = 0;
        var ID = req.query.id || -1;
        try {
            if (ID === -1) {
                throw new Error('id不能为空!');
            }
        } catch (e) {
            model.content = e.message;
            model.msg = e.message;
            return res.send(model);
        }

        NewsModel.getNewsByID(ID).then(function (news) {
            var model = new JsonModel.JsonModel();
            var newss = news[0];
            if (!newss) {
                model.msg = '没有这个ID';
                model.content = {};
            } else {
                model.msg = '成功';
                model.content = newss;
                model.status = 1;
            }
            res.send(model);
        })
    });

}